export default {
    data() {
        return {
            treeList: [],
            treeHost: "localhost",
            treePath:"cate",
            treeClassifys: "",
            treeSelectedObj: {},
            model: 'upload'
        }
    },
    methods: {
        //获取tree被选中的节点的对象
        onSelectChange() {
            let selectedObj = this.$refs.tree.getSelectedNodes();
            this.treeSelectedObj = selectedObj[0];
        },
        //获取tree数据
        getTreeData: function () {
            //获取表单数据
            this.axios({
                method:'get',
                url: `http://${this.treeHost}:3000/${this.treePath}/tree${this.treeClassifys}`
            }).then( res =>{
                
                this.treeList = res.data;
            });
        },
        //对话框提交按钮
        handleSubmit() {
             let newData = {...this.formValidate};//将表单数据深度拷贝到newData中，以便删除_id时，不会影响到this.formValidate
            let method = 'post';
            let url = `http://${this.treeHost}:3000/${this.treePath}/data`;
            //根据——id判断是否新增，是则删除_id字段
            if(newData._id.trim().length>0){
                url = url + '/' + newData._id;
                method = 'put';
            }else {
                delete newData._id;
            } 
            
            console.log(url)
            //判断parentId是否为空
            if(newData.parentId.trim().length<=0){
                delete newData.parentId;
            }
            //ajax操作
            this.axios({
                method,
                url,
                data: newData
            }).then( res =>{
                console.log(111)
                this.getTreeData();
                this.formState = false;
            });
        },
        //新增按钮点击事件
        createTreeBtnClick() {
            //重置对话框数据
            this.handleReset('formValidate');
            //获取点击节点
            let selectedNode = this.$refs.tree.getSelectedNodes();
            if(selectedNode.length > 0) {
                this.formValidate.parentId = selectedNode[0]._id;
                this.formValidate.type = selectedNode[0].type;
            }
            //显示对话框
            this.formState = true;
        },
        //更新按钮点击方法
        updataTreeBtnClick() {
            //重置对话框数据
            this.handleReset('formValidate');
            //获取点击节点
            let selectedNode = this.$refs.tree.getSelectedNodes();
            if(selectedNode.length > 0) {
                //显示对话框
                this.formState = true;
                this.formValidate._id = selectedNode[0]._id;
                this.formValidate.title = selectedNode[0].title;
                this.formValidate.type = selectedNode[0].type.toString();
                this.formValidate.parentId = selectedNode[0].parentId == null ? '' : selectedNode[0].parentId.toString();
            } else {
                this.$Modal.info({
                    title: '提示',
                    content: '请选择树节点'
                });
            }
        },
        //删除数据
        delTreeData() {
            let selectedNode = this.$refs.tree.getSelectedNodes();

            //判断是否选择要删除的数据
            if(selectedNode.length > 0) {
                //对话框  询问是否要删除数据
                this.$Modal.confirm({
                    title: '删除数据',
                    content: '<p>真的要删除这条数据吗</p>',
                    onOk: () => {
                        //ajax操作
                        this.axios({
                            method:'delete',
                            url: `http://${this.treeHost}:3000/${this.treePath}/data/${selectedNode[0]._id}`
                        }).then( res =>{
                            this.getTreeData();
                            this.formState = false; 
                        });
                    }
                });}
            else {
                //提示 选择节点
                this.$Modal.info({
                    title: '提示',
                    content: '请选择树节点'
                });
            }
            
        }
    }
}